package com.jiangnan.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jiangnan.entity.Book
import com.jiangnan.entity.Res
import com.jiangnan.repository.InteractionRepository
import kotlinx.coroutines.launch

class InteractionViewModel : ViewModel(){

    val interactionRepository = InteractionRepository()

    private val _markFavoriteResponse = MutableLiveData<Res<Any>>()
    val markFavoriteResponse: MutableLiveData<Res<Any>> get() = _markFavoriteResponse

    private val _updateRatingResponse = MutableLiveData<Res<Any>>()
    val updateRatingResponse: MutableLiveData<Res<Any>> get() = _updateRatingResponse

    private val _borrowBookResponse = MutableLiveData<Res<Any>>()
    val borrowBookResponse: MutableLiveData<Res<Any>> get() = _borrowBookResponse

    private val _toastMessage = MutableLiveData<String>()
    val toastMessage : MutableLiveData<String> get() = _toastMessage

    private val _favoriteBooks = MutableLiveData<List<Book>>()
    val favoriteBooks: LiveData<List<Book>> get() = _favoriteBooks

    private val _cancelFavoriteResponse = MutableLiveData<Res<Any>>()
    val cancelFavoriteResponse: MutableLiveData<Res<Any>> get() = _cancelFavoriteResponse



    fun getFavoriteBooks(userId: Int) {
        viewModelScope.launch {
            try {
                val response: Res<List<Book>> = interactionRepository.fetchFavoriteBooks(userId)
                if (response.code == 1) {
                    _favoriteBooks.postValue(response.data ?: emptyList())
                } else {
                    _toastMessage.postValue("获取收藏图书失败: ${response.msg}")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _toastMessage.postValue("获取收藏图书异常: ${e.message}")
            }
        }
    }

    fun markFavorite(userId: Int, bookId: Int) {
        viewModelScope.launch {
            try {
                val response = interactionRepository.markFavorite(userId, bookId)
                if (response.code == 1){
                    _toastMessage.postValue("收藏成功")
                } else {
                    _toastMessage.postValue("收藏失败")
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

    fun cancelFavorite(userId: Int, bookId: Int) {
        viewModelScope.launch {
            try {
                val response = interactionRepository.cancelFavorite(userId, bookId)
                if (response.code == 1){
                    _toastMessage.postValue("取消收藏成功")
                } else {
                    _toastMessage.postValue("取消收藏失败")
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

    fun updateRating(userId: Int, bookId: Int, rating: Double) {
        viewModelScope.launch {
            try {
                val response = interactionRepository.updateRating(userId, bookId, rating)
                if (response.code == 1){
                    _toastMessage.postValue("评分成功")
                } else {
                    _toastMessage.postValue("评分失败")
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

    fun borrowBook(userId: Int, bookId: Int) {
        viewModelScope.launch {
            try {
                val response = interactionRepository.borrowBook(userId, bookId)
                if (response.code == 1){
                    _toastMessage.postValue("借阅成功")
                } else {
                    _toastMessage.postValue("借阅失败")
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

}